<?php

/**
 *  值日
 */

class Controller_duty extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->syllabus = YOU::loadModel('syllabus');
		$this->model->grade    = YOU::loadModel('grade');
		$this->model->class    = YOU::loadModel('class');
		$this->model->subject  = YOU::loadModel('subject');
		$this->model->staff    = YOU::loadModel('staff');
		$this->model->semester = YOU::loadModel('semester');
		$this->model->duty     = YOU::loadModel('duty');
		$this->model->excel    = YOU::loadModel('excel');
		$this->model->log      = YOU::loadModel('log');
		
	}


	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}


	/** 添加值日表 **/
	public function actionAdd()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 最新学期id
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		// 本次id
		$box_id = $this->model->duty->getBox($semester_id);

		if ($_POST){

			// 生效日期
			$start_time = $this->post->start_time;
			$week_end   = $this->post->week_end;

			if ($week_end == '1'){
				$wend = 6;
			}else{
				$wend = 5;
			}
			$box_id = $box_id + 1;

			// 取得行数
			$line_lists = array();
			foreach ($_POST as $key => $value){
				if (substr($key, 0, 3) == "ar_"){
					$line_lists[] = substr($key, 3);
				}
			}

			// 行数
			foreach ($line_lists as $line){
				$duty_area = $_POST["ar_{$line}"];
				if (!empty($duty_area)){
					// 星期
					for ($w=1; $w<=$wend ;$w++){

						// 上午下午
						for ($a=1; $a<=2; $a++){

							$info = array(
							"semester_id"   => $semester_id,
							"box_id"        => $box_id,
							"duty_area"     => $duty_area,
							"duty_line"     => $line,
							"duty_week"     => $w,
							"duty_section"  => $a,
							"staff_id"      => $_POST["dy{$w}_{$a}_{$line}"],
							"status"        => 1,
							"start_time"    => $start_time,
							"create_time"   => date("Y-m-d H:i:s"),
							"update_time"   => "0000-00-00 00:00:00",
							);
							$this->db->do_insert("tbl_duty", $info);
						}
					}
				}
			}


			// 跳转
			Helper_Client::location("修改成功", "index.php?a=duty.info");

		}

		$duty_lists = $this->model->duty->getAllByBox($box_id);
		// 默认有一行
		if (empty($duty_lists)){
			$data_check[0] = 1;
		}else{

			// 自动复制前一box_id的数据选中
			$w = array();
			foreach ($duty_lists as $key => $value){
				$data_check[$value['duty_line']]['area'] = $value['duty_area'];
				$data_check[$value['duty_line']]["dy{$value['duty_week']}_{$value['duty_section']}"] = $value['staff_id'];

				$w[$value['duty_week']]++;
			}

			// 是否周末
			$this->tpl->assign("w", $w);

			$start_time = $value['start_time'];
			if (strtotime($start_time) > strtotime("now")){
				$this->tpl->assign("start_time", $start_time);
			}
		}

		// 教师列表
		$staff_lists = $this->model->staff->getAll();
		//var_dump($data_check);
		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->assign("data_check", $data_check);
		$this->tpl->display("duty/duty.add.tpl.php");
	}


	/** 往期值日表列表 **/
	public function actionList()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$semester_id = $this->get->semester_id;
		$box_id      = $this->get->box_id;

		$semester_data = $this->model->semester->getInfo($semester_id);
		// 教师姓名
		$staff = $this->model->staff->getIni();

		$duty_lists = $this->model->duty->getAllByBox($box_id);
		foreach ($duty_lists as $key => $value){
			$week_lists[$value['duty_week']] = $this->week_ini[$value['duty_week']];
			$data_lists[$value['duty_line']]['duty_area'] = $value['duty_area'];
			$data_lists[$value['duty_line']]['duty_data'][$value['duty_week']][$value['duty_section']] = $staff[$value['staff_id']];
		}
		$start_time = $value['start_time'];

		// 生效日期列表
		$start_lists = $this->model->duty->getStartList();

		// 学期列表
		//$semester_lists = $this->model->semester->getAll();

		$this->tpl->assign("start_time", $start_time);
		$this->tpl->assign("semester_id", $semester_id);
		$this->tpl->assign("box_id", $box_id);
		$this->tpl->assign("start_lists", $start_lists);
		//$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->assign("data_lists", $data_lists);
		$this->tpl->display("duty/duty.list.tpl.php");
	}


	/** 值日表信息 **/
	public function actionInfo()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 最新学期id
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		// 最新box_id
		$box_id = $this->model->duty->getBox($semester_id);
		$duty_lists = $this->model->duty->getAllByBox($box_id);

		// 教师姓名
		$staff = $this->model->staff->getIni();


		foreach ($duty_lists as $key => $value){
			$week_lists[$value['duty_week']] = $this->week_ini[$value['duty_week']];
			$data_lists[$value['duty_line']]['duty_area'] = $value['duty_area'];
			$data_lists[$value['duty_line']]['duty_data'][$value['duty_week']][$value['duty_section']] = $staff[$value['staff_id']];

			// 调动后得信息

		}


		// 生效日期
		$start_time = $value['start_time'];
		$start_time  = $value['start_time'];
		if (strtotime($start_time) > time() || $start_time=="0000-00-00" || empty($start_time)){
			$this->tpl->assign("is_update", "1");
		}
		$this->tpl->assign("start_time", $start_time);


		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->assign("data_lists", $data_lists);
		$this->tpl->display("duty/duty.info.tpl.php");
	}


	/** 值日表导出 **/
	public function actionOutData()
	{
		// 最新学期id
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		// 最新box_id
		$box_id = $this->model->duty->getBox($semester_id);
		$duty_lists = $this->model->duty->getAllByBox($box_id);

		// 教师姓名
		$staff = $this->model->staff->getIni();


		foreach ($duty_lists as $key => $value){
			$duty_week = $this->week_ini[$value['duty_week']];
			$section = $this->duty_section_ini[$value['duty_section']];
			$data[$value['duty_area']][$duty_week. $section] = $staff[$value['staff_id']];

			$week[$duty_week. $section]++;
			//var_dump($data);
		}
		// 生效日期
		$start_time = $value['start_time'];

		foreach ($data as $key => $value){

			$i = 1;
			$info = array();
			$info[0] = $key;
			foreach ($value as $k => $v){
				$info[$i] = $v;
				$i++;
			}

			$data_lists[] = $info;
		}

		$info = array();
		$info[] = "地点";
		foreach ($week as $key => $value){
			$info[] = $key;
		}
		//var_dump($data);exit;
		// 模板标题
		$this->model->excel->excel_ini['duty']['col'] = $info;
		$this->model->excel->excel_ini['duty']['title'] = "学校{$semester_data['semester_name']} 值日表";
		$this->model->excel->outData("duty", $data_lists);

	}

	/** 值日调动 **/
	public function actionChangeAdd()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 本学期最后一次课程表
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		$box_id = $this->model->duty->getBox($semester_id);
		$duty_lists = $this->model->duty->getAllByBox($box_id);

		if ($_POST){
			$change_area    = $this->post->change_area;
			$change_week    = $this->post->change_week;
			$change_section = $this->post->change_section;
			$change_start   = $this->post->change_start;
			$change_note    = $this->post->change_note;
			$change_to      = $this->post->change_to;
			$change_box     = $this->post->change_box;
			$change_from    = "";

			// 读出change_from
			foreach ($duty_lists as $key => $value){
				if ($value['duty_area'] == $change_area && $value['duty_week'] == $change_week && $value['duty_section']==$change_section){
					$change_from = $value['staff_id'];
				}
			}
			if (empty($change_from) ){
				Helper_Client::alert("选择错误");
			}
			if ($change_from == $change_to){
				Helper_Client::alert("调动重复");
			}


			// 修改上一次调动结束时间为当前开始时间
			$change_last = $this->db->fetchRow("SELECT * FROM `tbl_duty_change` WHERE change_area='{$change_area}' AND change_week='{$change_week}' AND change_section='{$change_section}' ORDER BY change_id DESC");
			if (!empty($change_last)){
				$info = array(
				"change_end"  => $change_start,
				"update_time" => date("Y-m-d H:i:s"),
				);
				$this->db->do_update("tbl_duty_change", $info, "change_id='{$change_last['change_id']}'");
				$change_from = $change_last['change_to'];
			}


			// 取得教师名
			$change_fromname = $this->model->staff->getNameById($change_from);
			$change_toname   = $this->model->staff->getNameById($change_to);

			// 写入数据库
			$info = array(
			"change_box"      => $change_box,
			"semester_id"     => $semester_id,
			"change_area"     => $change_area,
			"change_week"     => $change_week,
			"change_section"  => $change_section,
			"change_from"     => $change_from,
			"change_fromname" => $change_fromname,
			"change_to"       => $change_to,
			"change_toname"   => $change_toname,
			"change_start"    => $change_start,
			"change_end"      => "",
			"change_note"     => $change_note,
			"status"          => 1,
			"create_time"     => date("Y-m-d H:i:s"),
			"update_time"     => "0000-00-00 00:00:00",
			);
			$this->db->do_insert("tbl_duty_change", $info);


			// 跳转
			Helper_Client::location("修改成功", "index.php?a=duty.changelist");

		}


		foreach ($duty_lists as $key => $value){
			// 地点列表
			$area_lists[$value['duty_area']] = 1;

			// 星期列表
			$week_lists[$value['duty_week']] = $this->week_ini[$value['duty_week']];
		}


		// 教师列表
		$staff_lists = $this->model->staff->getAll();
		//var_dump($data_check)
		$this->tpl->assign("box_id", $box_id);
		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->assign("area_lists", $area_lists);
		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->display("duty/duty.changeadd.tpl.php");

	}


	/** 调动列表 **/
	public function actionChangeList()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 学年学期 如果为空取最新学期
		$semester_id = (int)$this->get->semester_id;
		$semester_data = $this->model->semester->getInfo($semester_id);

		if (empty($semester_data)){
			$semester_data = $this->model->semester->getNew();
			$semester_id   = $semester_data['semester_id'];
		}
		$where = " WHERE semester_id='{$semester_id}' ";

		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_duty_change` {$where} ");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$change_lists = $this->db->fetchAll("SELECT * FROM `tbl_duty_change` {$where} ORDER BY change_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($change_lists as $key => $value){
			$change_lists[$key]['change_week'] = $this->week_ini[$value['change_week']];
			if ($value['change_section'] == '1'){
				$change_lists[$key]['change_section'] = "上午";
			}
			if ($value['change_section'] == '2'){
				$change_lists[$key]['change_section'] = "下午";
			}
		}


		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		// 学期列表
		$semester_lists = $this->model->semester->getAll();

		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("semester_id", $semester_id);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("change_lists", $change_lists);
		$this->tpl->display("duty/duty.changelist.tpl.php");
	}


	/** 值日统计 **/
	public function actionStat()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		$box_id     = $this->model->duty->getBox($semester_id);
		$duty_lists = $this->model->duty->getAllByBox($box_id);
		$staff      = $this->model->staff->getIni();

		$week_lists = $this->db->fetchAll("SELECT * FROM tbl_week WHERE semester_id='{$semester_id}'");
		// 总周数
		$week_count = count($week_lists);


		// 生成模板
		foreach ($duty_lists as $key => $value){
			foreach ($week_lists as $k => $v){
				$s = $k+1;
				$staff_name = $staff[$value['staff_id']];
				$data_lists[$staff_name][$v['semester_month']]['week'][$s] = 0;
			}
		}

		foreach ($duty_lists as $key => $value){
			foreach ($week_lists as $k => $v){
				$staff_name = $staff[$value['staff_id']];
				if ($k == '0' || $k == $week_count){
					// 取得开始时间 结束时间
					$w_start = date("w", $v['start_time']);
					$w_end   = date("w", $v['end_time']);


					if ($value['duty_week'] >= $w_start && $value['duty_week'] <= $w_end){
						// 第一周 和最后一周实际计算
						$data_lists[$staff_name][$v['semester_month']]['week'][$v['week_number']]++;
						$data_lists[$staff_name][$v['semester_month']]['month']++;
					}
				}else{
					$data_lists[$staff_name][$v['semester_month']]['week'][$v['week_number']]++;
					$data_lists[$staff_name][$v['semester_month']]['month']++;
				}
			}

		}


		foreach ($week_lists as $key => $value){
			$week[$value['semester_month']][] = $value['week_number'];
		}


		$this->tpl->assign("week", $week);
		$this->tpl->assign("data_lists", $data_lists);
		$this->tpl->display("duty/duty.stat.tpl.php");
	}

}

?>